"""
File Name: Pycharm
Description:   job
Author: Panwj
Date: 2025/9/9 20:05
"""
from urllib.parse import urlsplit

from wtforms.fields.simple import StringField
from wtforms.form import Form
from wtforms.validators import DataRequired, ValidationError


class JobForm(Form):
    SITE_NAME_LIST = ("TW", "YT", "TK", "INS", "FB", "RD")
    ACTION_TYPE = ("LIKE_COMMENT", "REPLY_COMMENT", "REPLY_POST", "LIKE_POST")

    site_name = StringField(validators=[DataRequired()])
    action_type = StringField(validators=[DataRequired()])
    comment_content = StringField()  # 评论内容
    by_comment_content = StringField()  # 被评论的内容
    url = StringField(validators=[DataRequired()])  # 主贴url

    def validate_site_name(self, filed):
        """
        校验用户名是否重复
        """
        if filed.data not in self.SITE_NAME_LIST:
            raise ValidationError(message=f"site_name must be [{self.site_name}]")

    def validate_comment_content(self, filed):
        """
        校验评论内容
        :param filed:
        :return:
        """
        if self.action_type == 'REPLY_COMMENT' and not filed.data:
            raise ValidationError(message=f"type is reply comment, comment_content can't empty!")

    def validate_by_comment_content(self, filed):
        """
        校验评论内容
        :param filed:
        :return:
        """
        if self.action_type in ['REPLY_COMMENT', 'LIKE_COMMENT'] and not filed.data:
            raise ValidationError(message=f"type is reply comment, comment_content can't empty!")

    def validate_url(self, filed):
        """
        校验传入的主贴url
        :param filed:
        :return:
        """
        validate_domain = ['x.com', 'www.x.com', 'twitter.com', 'www.twitter.com', 'youtube.com', 'www.youtube.com','www.reddit.com']
        url_split = urlsplit(filed.data)
        if url_split.netloc not in validate_domain:
            raise ValidationError(message=f"url domain should be {validate_domain}")
